<?php

namespace app\admin\model\kuerp;

use think\Model;


class Prefix extends Model
{

    // 表名
    protected $name = 'kuerp_prefix';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;

    // 追加属性
    protected $append = [
        'year_text',
        'month_text',
        'day_text',
        'hour_text',
        'minute_text',
        'second_text'
    ];

    
    public function getYearList()
    {
        return ['1' => __('Year 1'), '2' => __('Year 2')];
    }

    public function getMonthList()
    {
        return ['1' => __('Month 1'), '2' => __('Month 2')];
    }

    public function getDayList()
    {
        return ['1' => __('Day 1'), '2' => __('Day 2')];
    }

    public function getHourList()
    {
        return ['1' => __('Hour 1'), '2' => __('Hour 2')];
    }

    public function getMinuteList()
    {
        return ['1' => __('Minute 1'), '2' => __('Minute 2')];
    }

    public function getSecondList()
    {
        return ['1' => __('Second 1'), '2' => __('Second 2')];
    }


    public function getYearTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['year']) ? $data['year'] : '');
        $list = $this->getYearList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getMonthTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['month']) ? $data['month'] : '');
        $list = $this->getMonthList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['day']) ? $data['day'] : '');
        $list = $this->getDayList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHourTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['hour']) ? $data['hour'] : '');
        $list = $this->getHourList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getMinuteTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['minute']) ? $data['minute'] : '');
        $list = $this->getMinuteList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSecondTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['second']) ? $data['second'] : '');
        $list = $this->getSecondList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 获取编号
     * @param int $type   编号类型
     * @return mixed
     */
    function getCodes($code,$type)
    {
        $paramscode = $code;
        $set = $this->where(['type'=>$type])->find();
        if(empty($set)){
            return '';
        }else{
            preg_match_all("/\d+/",$code,$num);
            $order_num = implode('',$num[0]);
            if($order_num<$set['start']){
                $order_num = $set['start'];
            }else{
                $order_num = (float)$order_num+1;
            }
            $prefix = $set['prefix'];
            if($set['year']==1){
                $prefix = $prefix.date('Y');
            }
            if($set['month']==1){
                $prefix = $prefix.date('m');
            }
            if($set['day']==1){
                $prefix = $prefix.date('d');
            }
            if($set['hour']==1){
                $prefix = $prefix.date('H');
            }
            if($set['minute']==1){
                $prefix = $prefix.date('i');
            }
            if($set['second']==1){
                $prefix = $prefix.date('s');
            }
            $s = '';
            if(strlen($order_num)<$set['strlen']){
                for ($a=1;$a<=$set['strlen']-(strlen($order_num));$a++){
                    $s .= '0';
                }
            }
            $code = $order_num;
            $order_num = $prefix.$s.$order_num;
        }

        if(!empty($set['dbname']) && !empty($set['field'])){
            if (empty($paramscode)){
                //获取数据库最后一个
                $lastcode = \think\Db::name($set['dbname'])->order('id desc')->value($set['field']);
                if ($lastcode) {
                    $lastcode = (int)str_replace($prefix,'',$lastcode);
                    $lastcode += 1;
                    $s = '';
                    if(strlen($lastcode)<$set['strlen']){
                        for ($a=1;$a<=$set['strlen']-(strlen($lastcode));$a++){
                            $s .= '0';
                        }
                    }
                    $code = $lastcode;
                    $order_num = $prefix.$s.$lastcode;
                }
            }

            $res = \think\Db::name($set['dbname'])->where([$set['field']=>$order_num])->value('id');
            if($res){
                return $this->getCodes($code,$type);
            }
        }
        return $order_num;
    }

}
